package mwa.cs.li.ctrl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mwa.cs.dao.SMApplicationLogDAO;
import mwa.cs.dao.UserLoginDAO;
import mwa.cs.database.Database;
import mwa.cs.model.SMApplicationLog;
import mwa.cs.util.Encryption;
import mwa.cs.util.JDate;




/**
 * Servlet implementation class LoginSrvl
 */
public class LoginSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginSrvl() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Database db = new Database();
		UserLoginDAO voUserLoginDAO = new UserLoginDAO(db);
		Encryption voEncryption = new Encryption();
		Map<String, Object> voMap = new HashMap<String, Object>();
		HttpSession voSession = request.getSession();
		String paUserName = request.getParameter("username");
		String vaPage ="";
		request.setAttribute("UserName", paUserName);
		String paUserPassword = voEncryption.md5Encrypt(voEncryption
				.dataEncrypt(request.getParameter("password")));

		voMap = voUserLoginDAO.Login(paUserName, paUserPassword);
		if (voMap == null) {
			request.setAttribute("vaLoginMessageError", "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง");
			
			RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
			rd.forward(request, response);
		} else {
			if (voMap.get("USER_STATUS").toString().equals("0")){
				request.setAttribute("vaLoginMessageError", "บัญชีผู้ใช้นี้ถูกระงับการใช้งาน");
				
				RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
				rd.forward(request, response);		
			}else{
				JDate voJDate = new JDate();
				String vaDate = voJDate.getDate();
				String vaTime = voJDate.getTime();
			
				SMApplicationLog voLog = new SMApplicationLog(0, "Login", JDate.getDate(), JDate.getTime(), 
						Integer.parseInt(voMap.get("USER_ID").toString()), "Login", "Username : "+  voMap.get("USER_NAME").toString()+" เข้าสู่ระบบ", 
						request.getParameter("url_address").toString(), request.getParameter("agent_browser").toString());
				SMApplicationLogDAO voSMApplicationLog = new SMApplicationLogDAO(db);
				voSMApplicationLog.addLog(voLog);
				
				int pnUserId = Integer.parseInt(voMap.get("USER_ID").toString());
				voSession.setAttribute("site_id", voMap.get("SITE_ID").toString());
				voSession.setAttribute("user_id", voMap.get("USER_ID").toString());
				voSession.setAttribute("vaUserId", voMap.get("USER_ID").toString());
				voSession.setAttribute("vaUserName", voMap.get("USER_NAME").toString());
				voSession.setAttribute("vaPassword", voMap.get("USER_PASSWORD").toString());
				voSession.setAttribute("vaAddress", voMap.get("EMAIL_ADDRESS").toString());
				voSession.setAttribute("vaRoleID", voMap.get("ROLE_ID").toString());
				voSession.setAttribute("vaRoleName", voMap.get("ROLE_NAME").toString());
				voSession.setAttribute("vaPosition", voMap.get("USER_POSITION").toString());
				voSession.setAttribute("vaUserPname", voMap.get("USER_PNAME").toString());
				voSession.setAttribute("vaUserFname", voMap.get("USER_FNAME").toString());
				voSession.setAttribute("vaUserLname", voMap.get("USER_LNAME").toString());
				voSession.setAttribute("vaURLAddress", request.getParameter("url_address").toString());
				voSession.setAttribute("vaAgentBrowser", request.getParameter("agent_browser").toString());
				voUserLoginDAO.updateLoginKey(pnUserId);
				voUserLoginDAO.updateUserLogin(vaDate,vaTime,pnUserId);
				voSession.setAttribute("vaLoginDate", vaDate.substring(6,8)+"/"+vaDate.substring(4,6)+"/"+vaDate.substring(0,4));
				voSession.setAttribute("vaLoginTime", vaTime.substring(0,2)+":"+vaTime.substring(2,4)+":"+vaTime.substring(4,6));
				voSession.setAttribute("vaLoginkey", voUserLoginDAO.findLoginKeyByUserId(pnUserId));
				response.sendRedirect("news/news.jsp");
			}
		}
		
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
